package com.fare.taluo.web.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONArray;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.fare.taluo.BaseFdcAction;
import com.fare.taluo.application.crawl.TaskAssignDayApplication;
import com.fare.taluo.application.crawl.TaskServiceApplication;
import com.fare.taluo.application.crawl.crawlstatistics.CrawlStatisticsTaskApplicaion;
import com.fare.taluo.application.crawl.crawlstatistics.FdcCrawlStatisticsApplication;
import com.fare.taluo.application.plane.channel.SourceApplication;
import com.fare.taluo.application.plane.city.PortApplication;
import com.fare.taluo.vo.core.StatisticsTaskEntityVo;
import com.fare.taluo.vo.core.StatisticsTotalDetailEntityVO;
import com.fare.taluo.vo.core.StatisticsTotalEntityVO;
import com.fare.taluo.vo.crawl.statistics.CrawlStatisticsVO;
import com.fare.taluo.vo.crawl.statistics.HeadCrawlStatisticsReport;
import com.fare.taluo.vo.crawl.statistics.SingleLineCrawlStatisticsVO;

@Controller("crawlInfoAction")
@Scope("prototype")
public class CrawlInfoAction extends BaseFdcAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = -9110235596771084173L;

	@Resource(name = "crawlStatisticsTaskApplicaion")
	private CrawlStatisticsTaskApplicaion crawlStatisticsTaskApplicaion;
	
	@Resource(name ="taskServiceApplication")
	private TaskServiceApplication taskServiceApplication;
	
	@Resource(name = "portApplication" )
	private PortApplication portApplication;
	
	@Resource(name = "sourceApplication")
	private SourceApplication sourceApplication;
	
	@Resource(name ="taskAssignDayApplication")
	private TaskAssignDayApplication taskAssignDayApplication;
	
	@Resource(name ="fdcCrawlStatisticsApplication")
	private FdcCrawlStatisticsApplication fdcCrawlStatisticsApplication;
	
	private List<StatisticsTaskEntityVo> volist = new ArrayList<StatisticsTaskEntityVo>();
	private List<StatisticsTotalEntityVO> totalList=new ArrayList<StatisticsTotalEntityVO>();
	private List<StatisticsTotalEntityVO> linesList=new ArrayList<StatisticsTotalEntityVO>();
	private List<CrawlStatisticsVO> allDatas=new ArrayList<CrawlStatisticsVO>();
	private HeadCrawlStatisticsReport headVo=new HeadCrawlStatisticsReport();
	
	private List<StatisticsTotalDetailEntityVO> singleNowsDatas=new ArrayList<StatisticsTotalDetailEntityVO>();
	private List<SingleLineCrawlStatisticsVO> singleStatusList=new ArrayList<SingleLineCrawlStatisticsVO>();
	private List<SingleLineCrawlStatisticsVO> singleLineCrawlStatisticsVOList=new ArrayList<SingleLineCrawlStatisticsVO>();
	
	
	private List<StatisticsTotalDetailEntityVO> finishedDatas;
		private JSONArray ja = new JSONArray();
	
	private Map<String,Integer > othersFlight=new HashMap<String,Integer>();
	private List<String > listStr=new ArrayList<String>();
	private Map<String ,StatisticsTotalDetailEntityVO> mapVo=new HashMap<String ,StatisticsTotalDetailEntityVO>();
	private List<String > ListVO=new ArrayList<String>();
	JSONArray json =new JSONArray();
	JSONArray jsonMap =new JSONArray();
	private String Reportgrabchannel;//渠道
	private String beginTime="";//查询范围-开始时间
	private String endTime="";//查询范围-结束时间
	private String grabdate;//抓取日期
	private String grabdateEnd="";//抓取日期段
	private String grabchannelID="";//渠道ID
	
	private String depCity;
	private String arrCity;
	private String depCityName;
	private String arrCityName;
	private Long totalSourceSizes;
	private String grabDate_1;
	private String grabDate_2;
	
	private String indexNumber;
	private String indexs;
	private String finishedStartTimeStr;
	private String finishedEndTimeStr;
	private String successRate;
	private String dataTotalRate;
	
	private String sigleTotalSize;
	private String sigleSuccessRate;
	private String sigleDataTotalRate;
	
	//航线组件
	private String fromCityCode;
	private String fromCity;
	private String toCityCode;
	private String toCity;
	
	//抓取统计
	public String getCrawlStatisticsReport(){
		try {
			//按日期
			if(toCity !=null && !"".equals(toCity) && toCity.indexOf("/") ==-1 ){
				arrCity=toCity.split("\\(")[1].split("\\)")[0];
				arrCityName=toCity.split("\\(")[0];
			}
			if(fromCity !=null && !"".equals(fromCity) && fromCity.indexOf("/") ==-1){
				depCity=fromCity.split("\\(")[1].split("\\)")[0];
				depCityName=fromCity.split("\\(")[0];
			}

			this.allDatas=crawlStatisticsTaskApplicaion.showByGrabDate(grabDate_1, grabDate_2);
			this.linesList=this.crawlStatisticsTaskApplicaion.getDataByLine(depCity, arrCity);
			if(allDatas.size()>0){
				long allSourceSizesLong=0l;
				//int allLineSizesInt=0;
				Map<String ,Integer> channelSizes=new HashMap<String ,Integer>();
				int sizes=0;
				for(int allDataSizes=0;allDataSizes<allDatas.size();allDataSizes++){
					CrawlStatisticsVO vo=allDatas.get(allDataSizes);
					allSourceSizesLong+=vo.getAllSourceSizeLong();
				//	allLineSizesInt+=vo.getAllLines();
					int hbaseKesSizes=vo.getSourceSizeStr().split("</br>").length;
					for(int i=0;i<hbaseKesSizes;i++){
						String hbaseKey=vo.getSourceSizeStr().split("</br>")[i].split(" @ ")[1].split("<")[0];
						if(channelSizes.get(hbaseKey)==null){
							channelSizes.put(hbaseKey,sizes++);
						}
					}
				}
				headVo.setGrabs(channelSizes.size());
				headVo.setAllDataSizes(allSourceSizesLong+"");
				headVo.setAllDataSizeLongs(allSourceSizesLong);
//				headVo.setAllLines(allLineSizesInt);
				headVo.setAllLines(linesList.size());
				headVo.setEndDate(allDatas.get(0).getDateStr());
				headVo.setStartDate(allDatas.get(allDatas.size()-1).getDateStr());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		this.returnPageURL = "/fare/crawlStatistics/_crawlInfo.jsp";
		return "dispatcher";
	}
	
//	/*
//	 * 单航线抓取统计
//	 */	
//	public String findSingleLine(){
//		String flag=null;
//		List<StatisticsTotalDetailEntityVO> singlelineList=new ArrayList<StatisticsTotalDetailEntityVO>();
//		grabdate=DateUtils.getDateStr("yyyy-MM-dd");//当天日期
//		flag=request.getParameter("flag");
//		if(flag !=null){
//			if(flag.equals("1")){
//				if(toCity !=null && !"".equals(toCity) && toCity.indexOf("/") ==-1 ){
//					arrCity=toCity.split("\\(")[1].split("\\)")[0];
//					arrCityName=toCity.split("\\(")[0];
//				}
//				if(fromCity !=null && !"".equals(fromCity) && fromCity.indexOf("/") ==-1){
//					depCity=fromCity.split("\\(")[1].split("\\)")[0];
//					depCityName=fromCity.split("\\(")[0];
//				}
//			}
//		}else{
//			//从菜单进入此页面(默认CAN-PEK)
//			if(depCity ==null || arrCity==null || arrCity.equals("") || depCity.equals("")){
//				depCity="CAN";
//				arrCity="PEK";
//			}
//			depCityName=portApplication.findCityNameByPortCode(depCity).getCityName();
//			arrCityName=portApplication.findCityNameByPortCode(arrCity).getCityName();
//		}
//		
//		//调用webServices接口
//		try {
//			singlelineList=taskServiceApplication.query(null,depCity, arrCity, null);
//		} catch (Exception e1) {
//			e1.printStackTrace();
//		}
//		
//		//从“抓取统计”页面进入页面
//		sigleTotalSize="0";
//		sigleSuccessRate="0%";
//		sigleDataTotalRate="0%";
//		long singleSuccessNumber=0l,singleFailNumber=0l,singleTotalDataSizes=0l;
//		if(singlelineList.size()>0){
//			this.singleNowsDatas=singlelineList;
//			for(int singleNumbers=0;singleNumbers<singleNowsDatas.size();singleNumbers++){
//				StatisticsTotalDetailEntityVO sigleVo=new StatisticsTotalDetailEntityVO();
//				sigleVo=singleNowsDatas.get(singleNumbers);
//				if(sigleVo.getStatus()==3){
//					singleSuccessNumber++;
//				}else{
//					singleFailNumber++;
//				}
//				singleTotalDataSizes+=sigleVo.getSourceDataSize();
//			}
//			//过滤抓取日期（使用webServices接口不用过滤）	
//			List<StatisticsTotalDetailEntityVO> singleFilterVoList =new ArrayList<StatisticsTotalDetailEntityVO>();
//			singleFilterVoList=singleNowsDatas;
//			if(singleFilterVoList.size()>0){
//				Map<String,List<StatisticsTotalDetailEntityVO>> mapKey=new HashMap<String,List<StatisticsTotalDetailEntityVO>>();
//				for(StatisticsTotalDetailEntityVO vo:singleFilterVoList){
//					String key=vo.getFlightDate();
//					if(mapKey.get(key) !=null){
//						mapKey.get(key).add(vo);
//					}else{
//						List<StatisticsTotalDetailEntityVO> listVOS=new ArrayList<StatisticsTotalDetailEntityVO>();
//						listVOS.add(vo);
//						mapKey.put(key, listVOS);
//					}
//				}
//				sigleDataTotalRate=(mapKey.size()/30 )*100+"%";
//			}
//		}
//		if(singleTotalDataSizes>0){
//			sigleTotalSize=singleTotalDataSizes/(1024*1024)+"";
//		}
//		if((singleSuccessNumber>0)){
//			sigleSuccessRate=(singleSuccessNumber*100)/(singleSuccessNumber+singleFailNumber)+"%";
//		}
//		//webServices
//		this.singleLineCrawlStatisticsVOList = taskServiceApplication.listCrawlStatisticsVo(singlelineList);
//		jsonMap= JSONArray.fromObject(singleLineCrawlStatisticsVOList); 
//		this.returnPageURL = "/fare/crawlStatistics/_singleLineCrawlInfo.jsp";
//		return "dispatcher";
//	}
	
	/*
	 * 历史数据页面view
	 */	
	public String findHistoryData(){
		
		depCityName=portApplication.findCityNameByPortCode(depCity).getCityName();
		arrCityName=portApplication.findCityNameByPortCode(arrCity).getCityName();
		//表格数据
		try {
			this.totalList=this.crawlStatisticsTaskApplicaion.getHistoryDatas(depCity, arrCity,grabDate_1, grabDate_2);
			totalSourceSizes=0L;
			if(totalList.size()>0){
				for(int i=0;i<totalList.size();i++){
					StatisticsTotalEntityVO vo=totalList.get(i);
					totalSourceSizes+=vo.getSourceDataSize();
				}
				totalSourceSizes=totalSourceSizes/(1024*1024);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		//图表数据
		ja.add("{\"xml\":\""
				+ com.fare.taluo.util.crawl.XmlUtil.getCrawlGrabHistroyXMLshow("抓取数据量统计表",totalList)+ "\"}");
		this.returnPageURL = "/fare/crawlStatistics/_singleLineCrawlHistoryInfo.jsp";
		return "dispatcher";
	}
	
	
	//已完成页面操作
/*	public String finishedCrawlDataView(){
		
		indexs=indexNumber;
		try {
			depCityName=URLDecoder.decode(depCityName, "UTF-8");
			depCity=URLDecoder.decode(depCity, "UTF-8");
			arrCityName=URLDecoder.decode(arrCityName, "UTF-8");
			arrCity=URLDecoder.decode(arrCity, "UTF-8");
			Reportgrabchannel=URLDecoder.decode(Reportgrabchannel,"UTF-8");
			finishedEndTimeStr=URLDecoder.decode(finishedEndTimeStr,"UTF-8");
			finishedStartTimeStr=URLDecoder.decode(finishedStartTimeStr,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		
		 * 根据depCity,arrCity ,Reportgrabchannel,finishedEndTimeStr,finishedStartTimeStr
		 * 组装已完成的数据统计
		 		
		
		grabdate=DateUtils.getDateStr("yyyy-MM-dd");//当天日期
		grabchannelID=sourceApplication.findSourceEntityByName(Reportgrabchannel).get(0).getId().toString();
		finishedStartTimeStr=finishedStartTimeStr.split("\\.")[0] ;
		finishedEndTimeStr=finishedEndTimeStr.split("\\.")[0];
		finishedDatas=new ArrayList<StatisticsTotalDetailEntityVO>();
		
		finishedDatas=taskServiceApplication.getDataForFinished(depCity, arrCity, grabchannelID,finishedStartTimeStr, finishedEndTimeStr);
		Long successNum=0l;
		Long failNum=0l;
		if(finishedDatas.size()>0){
			for(int i=0;i<finishedDatas.size();i++){
				StatisticsTotalDetailEntityVO vo=new StatisticsTotalDetailEntityVO();
				vo=finishedDatas.get(i);
				successNum+=vo.getSuccessNum();
				failNum+=vo.getFailNum();
				
				mapVo.put(vo.getFlightDate(), vo);
			}
		}
		
		if(mapVo.size()>0){
			json = JSONArray.fromObject(mapVo); 

			for(Map.Entry<String,StatisticsTotalDetailEntityVO> flightLists:mapVo.entrySet()){
				ListVO.add(flightLists.getKey());
			}
		}
		
		//判断是否含有非正常抓取
		 List<TaskInfoEntityVOVO> voList= InstanceFactory.getInstance(TaskTemplateApplication.class).findTaskInfoByConsdions(Reportgrabchannel, depCity, arrCity, 0, 0);
		 int td=0;
		 
		 if(voList.size()>0){
			 for(int j=0;j<voList.size();j++){
				 TaskInfoEntityVOVO vo=new TaskInfoEntityVOVO();
				 vo=voList.get(j);
				 Long id=vo.getId();
				 
				List<TaskAssignDayEntityVO> listTD= taskAssignDayApplication.findVoByTempId(id);
				for(int k=0;k<listTD.size();k++){
					TaskAssignDayEntityVO tasVo=listTD.get(k);
					if(tasVo.getAssignFlightDay()>30){
						String keys=tasVo.getAssignFlightDay()+"";
						int values=tasVo.getAssignFlightDay();
						othersFlight.put(keys, values);
						td++;
					}
				}
			 }
		 }
		if(othersFlight.size()>0){
			
			for(Map.Entry<String , Integer> flightDate:othersFlight.entrySet()){
				listStr.add(flightDate.getKey());
			}
			if(listStr.size()>0){
				Collections.sort(listStr);
			}
		}
		dataTotalRate=((finishedDatas.size()+td)/(td +30))*100+"";
		if(successNum >0){
			successRate=(successNum*100)/(failNum+successNum)+"";
		}else{
			successRate="0";
		}
		
		this.returnPageURL ="/fare/crawlStatistics/_singleLineFinishedCrawl.jsp";
		return "dispatcher";
	}*/
	
	public List<StatisticsTaskEntityVo> getVolist() {
		return volist;
	}


	public void setVolist(List<StatisticsTaskEntityVo> volist) {
		this.volist = volist;
	}


	public String getBeginTime() {
		return beginTime;
	}


	public void setBeginTime(String beginTime) {
		this.beginTime = beginTime;
	}


	public String getEndTime() {
		return endTime;
	}


	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}
	

	public String getGrabdate() {
		return grabdate;
	}


	public void setGrabdate(String grabdate) {
		this.grabdate = grabdate;
	}


	public String getGrabchannelID() {
		return grabchannelID;
	}


	public void setGrabchannelID(String grabchannelID) {
		this.grabchannelID = grabchannelID;
	}
	

	public List<StatisticsTotalEntityVO> getTotalList() {
		return totalList;
	}


	public void setTotalList(List<StatisticsTotalEntityVO> totalList) {
		this.totalList = totalList;
	}


	public List<CrawlStatisticsVO> getAllDatas() {
		return allDatas;
	}


	public void setAllDatas(List<CrawlStatisticsVO> allDatas) {
		this.allDatas = allDatas;
	}

	public HeadCrawlStatisticsReport getHeadVo() {
		return headVo;
	}

	public void setHeadVo(HeadCrawlStatisticsReport headVo) {
		this.headVo = headVo;
	}

	public List<StatisticsTotalEntityVO> getLinesList() {
		return linesList;
	}

	public void setLinesList(List<StatisticsTotalEntityVO> linesList) {
		this.linesList = linesList;
	}

	public String getDepCity() {
		return depCity;
	}

	public void setDepCity(String depCity) {
		this.depCity = depCity;
	}

	public String getArrCity() {
		return arrCity;
	}

	public void setArrCity(String arrCity) {
		this.arrCity = arrCity;
	}

	public List<SingleLineCrawlStatisticsVO> getSingleStatusList() {
		return singleStatusList;
	}

	public void setSingleStatusList(
			List<SingleLineCrawlStatisticsVO> singleStatusList) {
		this.singleStatusList = singleStatusList;
	}

	public List<SingleLineCrawlStatisticsVO> getSingleLineCrawlStatisticsVOList() {
		return singleLineCrawlStatisticsVOList;
	}

	public void setSingleLineCrawlStatisticsVOList(
			List<SingleLineCrawlStatisticsVO> singleLineCrawlStatisticsVOList) {
		this.singleLineCrawlStatisticsVOList = singleLineCrawlStatisticsVOList;
	}

	public List<StatisticsTotalDetailEntityVO> getSingleNowsDatas() {
		return singleNowsDatas;
	}

	public void setSingleNowsDatas(
			List<StatisticsTotalDetailEntityVO> singleNowsDatas) {
		this.singleNowsDatas = singleNowsDatas;
	}

	public String getDepCityName() {
		return depCityName;
	}

	public void setDepCityName(String depCityName) {
		this.depCityName = depCityName;
	}

	public String getArrCityName() {
		return arrCityName;
	}

	public void setArrCityName(String arrCityName) {
		this.arrCityName = arrCityName;
	}

	public String getGrabdateEnd() {
		return grabdateEnd;
	}

	public void setGrabdateEnd(String grabdateEnd) {
		this.grabdateEnd = grabdateEnd;
	}

	public JSONArray getJa() {
		return ja;
	}

	public void setJa(JSONArray ja) {
		this.ja = ja;
	}

	public Long getTotalSourceSizes() {
		return totalSourceSizes;
	}

	public void setTotalSourceSizes(Long totalSourceSizes) {
		this.totalSourceSizes = totalSourceSizes;
	}

	public String getIndexNumber() {
		return indexNumber;
	}

	public void setIndexNumber(String indexNumber) {
		this.indexNumber = indexNumber;
	}

	public String getIndexs() {
		return indexs;
	}

	public void setIndexs(String indexs) {
		this.indexs = indexs;
	}

	public String getFinishedStartTimeStr() {
		return finishedStartTimeStr;
	}

	public void setFinishedStartTimeStr(String finishedStartTimeStr) {
		this.finishedStartTimeStr = finishedStartTimeStr;
	}

	public String getFinishedEndTimeStr() {
		return finishedEndTimeStr;
	}

	public void setFinishedEndTimeStr(String finishedEndTimeStr) {
		this.finishedEndTimeStr = finishedEndTimeStr;
	}

	public String getReportgrabchannel() {
		return Reportgrabchannel;
	}

	public void setReportgrabchannel(String reportgrabchannel) {
		Reportgrabchannel = reportgrabchannel;
	}

	public String getSuccessRate() {
		return successRate;
	}

	public void setSuccessRate(String successRate) {
		this.successRate = successRate;
	}

	public String getDataTotalRate() {
		return dataTotalRate;
	}

	public void setDataTotalRate(String dataTotalRate) {
		this.dataTotalRate = dataTotalRate;
	}

	public Map<String, Integer> getOthersFlight() {
		return othersFlight;
	}

	public void setOthersFlight(Map<String, Integer> othersFlight) {
		this.othersFlight = othersFlight;
	}

	public List<String> getListStr() {
		return listStr;
	}

	public void setListStr(List<String> listStr) {
		this.listStr = listStr;
	}

	public Map<String, StatisticsTotalDetailEntityVO> getMapVo() {
		return mapVo;
	}

	public void setMapVo(Map<String, StatisticsTotalDetailEntityVO> mapVo) {
		this.mapVo = mapVo;
	}

	public List<String> getListVO() {
		return ListVO;
	}

	public void setListVO(List<String> listVO) {
		ListVO = listVO;
	}

	public JSONArray getJson() {
		return json;
	}

	public void setJson(JSONArray json) {
		this.json = json;
	}

	public String getSigleSuccessRate() {
		return sigleSuccessRate;
	}

	public void setSigleSuccessRate(String sigleSuccessRate) {
		this.sigleSuccessRate = sigleSuccessRate;
	}

	public String getSigleDataTotalRate() {
		return sigleDataTotalRate;
	}

	public void setSigleDataTotalRate(String sigleDataTotalRate) {
		this.sigleDataTotalRate = sigleDataTotalRate;
	}

	public String getSigleTotalSize() {
		return sigleTotalSize;
	}

	public void setSigleTotalSize(String sigleTotalSize) {
		this.sigleTotalSize = sigleTotalSize;
	}

	public JSONArray getJsonMap() {
		return jsonMap;
	}

	public void setJsonMap(JSONArray jsonMap) {
		this.jsonMap = jsonMap;
	}

	public String getGrabDate_1() {
		return grabDate_1;
	}

	public void setGrabDate_1(String grabDate_1) {
		this.grabDate_1 = grabDate_1;
	}

	public String getGrabDate_2() {
		return grabDate_2;
	}

	public void setGrabDate_2(String grabDate_2) {
		this.grabDate_2 = grabDate_2;
	}

	public String getFromCityCode() {
		return fromCityCode;
	}

	public void setFromCityCode(String fromCityCode) {
		this.fromCityCode = fromCityCode;
	}

	public String getFromCity() {
		return fromCity;
	}

	public void setFromCity(String fromCity) {
		this.fromCity = fromCity;
	}

	public String getToCityCode() {
		return toCityCode;
	}

	public void setToCityCode(String toCityCode) {
		this.toCityCode = toCityCode;
	}

	public String getToCity() {
		return toCity;
	}

	public void setToCity(String toCity) {
		this.toCity = toCity;
	}
	
}
